<?php

$app->get('/nav', function () use ($app, $c) {
    $tree = array(/*
        array('id' => 'all', 'name' => 'All', 'type' => 'all'),
        array('id' => 'daily', 'name' => 'Daily Review', 'parent' => 'all', 'type' => 'daily'),
        array('id' => 'weekly', 'name' => 'Weekly Review', 'parent' => 'all', 'type' => 'weekly'),
        array('id' => 'monthly', 'name' => 'Monthly Review', 'parent' => 'all', 'type' => 'monthly'),*/
    );

    $nodeTable = $c['Review.Table.Node'];

    $result = $nodeTable->select(function (\Zend\Db\Sql\Select $select) {
        $select->columns(array(
            'max_start_date' => new \Zend\Db\Sql\Expression("max(start_date)"),
            'min_start_date' => new \Zend\Db\Sql\Expression("max(start_date)"),
            'max_end_date' => new \Zend\Db\Sql\Expression("max(end_date)"),
            'min_end_date' => new \Zend\Db\Sql\Expression("max(end_date)"),
        ));
    })->current();

    header('Content-Type: application/json;charset=utf-8');
    $archived = array();
    $oneday = new DateInterval("P1D");

    $max_date = new DateTime(max($result->max_start_date, $result->max_end_date));
    $min_date = new DateTime(min($result->min_start_date, $result->min_start_date));
    for ($i = $min_date; $i <= $max_date; $i = $i->add($oneday)) {
        if (!isset($archived[$i->format('Y')])) {
            $archived[$i->format('Y')] = array();
        }
        if (!isset($archived[$i->format('Y')][$i->format('m')])) {
            $archived[$i->format('Y')][$i->format('m')] = array();
        }
        if (!isset($archived[$i->format('Y')][$i->format('m')][$i->format('d')])) {
            $archived[$i->format('Y')][$i->format('m')][$i->format('d')] = array();
        }
    }
    foreach ($archived as $year => $months) {
        $tree[] = array('id' => 'daily-' . $year, 'name' => $year, 'parent' => 'daily', 'type' => 'daily');
        $tree[] = array('id' => 'weekly-' . $year, 'name' => $year, 'parent' => 'weekly', 'type' => 'weekly');
        $tree[] = array('id' => 'monthly-' . $year, 'name' => $year, 'parent' => 'monthly', 'type' => 'monthly');
        foreach ($months as $month => $days) {
            $tree[] = array('id' => 'daily-' . $year . '-' . $month, 'name' => $month, 'parent' => 'daily-' . $year, 'type' => 'daily');
            $tree[] = array('id' => 'weekly-' . $year . '-' . $month, 'name' => $month, 'parent' => 'weekly-' . $year, 'type' => 'weekly');
            $tree[] = array('id' => 'monthly-' . $year . '-' . $month, 'name' => $month, 'parent' => 'monthly-' . $year, 'type' => 'monthly');
        }
        foreach ($days as $day => $no) {
            $tree[] = array('id' => 'daily-' . $year . '-' . $month . '-' . $day, 'name' => $day, 'parent' => 'daily-' . $year . '-' . $month, 'type' => 'daily');
            $tree[] = array('id' => 'weekly-' . $year . '-' . $month . '-' . $day, 'name' => $day, 'parent' => 'weekly-' . $year . '-' . $month, 'type' => 'weekly');
        }
    }
    echo json_encode($tree);
    exit;
})->name('review-nav');